﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Input;

namespace ERModelData
{
    public partial class MainWindow : Window
    {
        Grid grid;
        ScrollViewer scroll;
        Property property;
        Canvas canvas;

        public MainWindow()
        {
            InitializeComponent();

            /// Настройки Property
            property = new Property();
            Grid.SetColumn(property, 1);
            Grid.SetRow(property, 0);

            /// Настройки Canvas
            canvas = new Canvas(property);
            
            /// Настройки Scroll
            scroll = new ScrollViewer();
            scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            Grid.SetColumn(scroll, 0);
            Grid.SetRow(scroll, 0);
            scroll.Content = canvas;

            /// Настройки Grid
            grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions[0].Width = new GridLength(70, GridUnitType.Star);
            grid.ColumnDefinitions[1].Width = new GridLength(30, GridUnitType.Auto);
            grid.Children.Add(scroll);
            grid.Children.Add(property);
            
            /// Настройки Window
            this.Content = grid;
            this.Background = SystemColors.ControlBrush;
            this.SizeChanged += new SizeChangedEventHandler(Resize);
            this.WindowState = WindowState.Maximized;
        }

        void Resize(object sender, SizeChangedEventArgs e)
        {
            double Height = scroll.ActualHeight - 5;
            double Width = scroll.ActualWidth - 5;
            foreach(UIElement i in canvas.Children)
                if (i is Essence)
                {
                    Height = Height < Canvas.GetTop(i) + ((Border)i).Height ? 
                        Canvas.GetTop(i) + ((Border)i).Height : 
                        Height;
                    Width = Width < Canvas.GetLeft(i) + ((Border)i).Width ? 
                        Canvas.GetLeft(i) + ((Border)i).Width : 
                        Width;
                }
                else if (i is Line)
                {
                    Width = ((Line)i).X1 > ((Line)i).X2 ?
                        Width < ((Line)i).X1 ? ((Line)i).X1 : Width :
                        Width < ((Line)i).X2 ? ((Line)i).X2 : Width;
                    Height = ((Line)i).Y1 > ((Line)i).Y2 ?
                        Height < ((Line)i).Y1 ? ((Line)i).Y1 : Height :
                        Height < ((Line)i).Y2 ? ((Line)i).Y2 : Height;
                }
            canvas.Width = Width + 5;
            canvas.Height = Height + 5;
        }
    }
}
